package com.ww.disruptor.handler;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;
import com.ww.disruptor.event.ProductEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class ProductEventHandler implements EventHandler<ProductEvent>, WorkHandler<ProductEvent> {

    private Logger logger = LoggerFactory.getLogger(ProductEventHandler.class);

    @Override
    public void onEvent(ProductEvent event) throws Exception {
//        logger.info("集群模式，线程{}消费者开始处理event:{}", Thread.currentThread().getName(), event.getUid());
        System.out.println("集群模式，线程" + Thread.currentThread().getName() + "消费者开始处理event:" + event.getUid());

        logger.info("集群模式，消费者{}打印uid:{}", Thread.currentThread().getName(), event.getUid());
        TimeUnit.SECONDS.sleep(3);

//        logger.info("集群模式，线程{}消费者处理完毕", Thread.currentThread().getName());
        System.out.println("集群模式，线程" + Thread.currentThread().getName() + "消费者处理完毕");
    }

    @Override
    public void onEvent(ProductEvent event, long sequence, boolean endOfBatch) throws Exception {
//        logger.info("广播模式，线程{}消费者开始处理event:{}!", Thread.currentThread().getName(), event.getUid());
        System.out.println("广播模式，线程" + Thread.currentThread().getName() + "消费者开始处理event:" + event.getUid());

        System.out.println("广播模式，消费者" + Thread.currentThread().getName() + "打印uid:" + event.getUid());
        //保证生产快于消费
        Thread.sleep(3000);

//        logger.info("广播模式，线程{}消费者处理event完毕!", Thread.currentThread().getName());
        System.out.println("广播模式，线程" + Thread.currentThread().getName() + "消费者处理完毕");
    }
}
